/**
 * 
 */
package cc.comac.mom.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipCheckService;
import cc.comac.mom.bean.EquipCheck;
import cc.comac.mom.bean.EquipCheckBean;
import cc.comac.mom.bean.EquipCheckDetail;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.datahandlers.entity.EquipCheckDataHandler;
import cc.comac.mom.datahandlers.entity.EquipCheckDetailDataHandler;
import cc.comac.mom.util.ResultBean;

/**
* @Description: 
* @author: fyt 
* @date: 2020年7月21日下午2:48:35
*/
@RestController
@Service
public class EquipCheckServiceImpl implements EquipCheckService{

	@Autowired
	private EquipCheckDataHandler equipCheckDataHandler;
	@Autowired
	private EquipCheckDetailDataHandler equipCheckDetailDataHandler;
	@Override
	public ResultBean save(@RequestBody EquipCheckBean obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;	
		}
		EquipCheck equipCheck = obj.getEquipCheck();
		List<EquipCheckDetail> listCheckDetail = obj.getListCheckDetail();
		if(equipCheck == null || listCheckDetail == null || listCheckDetail.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;	
		}
		ResultBean save = equipCheckDataHandler.save(equipCheck);
		if(!save.success) {
			return save;
		}
		Long id = Long.valueOf(save.getMessage());
		for(EquipCheckDetail item:listCheckDetail) {
			item.getInnerData().setCheckId(id);
		}
		ResultBean saveBatch = equipCheckDetailDataHandler.saveBatch(listCheckDetail);
		if(!saveBatch.success) {
			throw new RuntimeException("保存盘点详情失败");
		}
		return resultBean;
	}
	
	
	@Override
	public ResultBean selectByCondition(@RequestBody EquipCheck obj) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev resultDev = equipCheckDataHandler.selectByCondition(obj);
		resultBean.setObject(resultDev);
		return resultBean;
	}
	@Override
	public ResultBean checkConfirm(@RequestBody List<Long> listId) {
		// TODO Auto-generated method stub
		return equipCheckDataHandler.checkConfirm(listId);
	}


	@Override
	public ResultBean update(@RequestBody EquipCheckBean obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;	
		}
		EquipCheck equipCheck = obj.getEquipCheck();
		List<EquipCheckDetail> listCheckDetail = obj.getListCheckDetail();
		if(equipCheck == null && listCheckDetail == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		if(equipCheck != null && equipCheck.getInnerData() != null) {
			ResultBean update = equipCheckDataHandler.update(equipCheck);
			if(!update.success) {
				return update;
			}
		}
		if(listCheckDetail != null && listCheckDetail.size()>0) {
			ResultBean saveBatch = equipCheckDetailDataHandler.saveBatch(listCheckDetail);
			if(!saveBatch.success) {
				return saveBatch;
			}
		}
		
		return resultBean;
	}


	@Override
	public ResultBean deleteBatch(@RequestBody List<EquipCheck> list) {
		ResultBean resultBean = new ResultBean(true);
		ResultBean deleteBatch = equipCheckDataHandler.deleteBatch(list);
		if(!deleteBatch.success) {
			return deleteBatch;
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		for(EquipCheck item:list) {
			listIds.add(item.getInnerData().getId());
		}
		List<EquipCheckDetail> listEquipCheckDetail = equipCheckDetailDataHandler.getByCheckIds(listIds);
		if(listEquipCheckDetail != null && listEquipCheckDetail.size()>0) {
			ResultBean deleteBatch2 = equipCheckDetailDataHandler.deleteBatch(listEquipCheckDetail);
			if(!deleteBatch2.success) {
				new RuntimeException(deleteBatch2.getMessage());
			}
		}
		return resultBean;
	}

}
